package com.itguigu.rabbitmq.five;

import com.itguigu.rabbitmq.utils.RabbitMqutil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Date 2021/7/18 20:55
 * @Created by wangle
 * @Description 消息接收
 */
public class receive1 {

    public static final String exchagenames = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqutil.getChannel();
        //声明交换机
        channel.exchangeDeclare(exchagenames, "fanout");
        //声明一个队列,临时队列，当消费者断开与队列的连接时，队列就会自动删除
        String queuename = channel.queueDeclare().getQueue();

        //绑定交换机与队列
        channel.queueBind(queuename, exchagenames, "");
        System.out.println("等待接收消息，把接收到的消息打印到屏幕上。。。。");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("01控制台打印接收到的消息：" + new String(message.getBody()));

        };

        //取消消息时的回调
        CancelCallback cancelCallback = (consumerTag) -> {

            System.out.println("消息消费被中断");
        };

        //后两个参数---接收消息，取消消息
        channel.basicConsume(queuename, true, deliverCallback, cancelCallback);

    }


}
